package cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager;

import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupMemberDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupMemberVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupMemberPositionEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupMemberDao;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Date 2019-01-26 19:32:01<br>
 * Description
 * 
 * @author yitun<br>
 * @version 1.1
 */
@Service
public class GroupMemberManager {

	@Resource
	private GroupMemberDao groupMemberDAO;

	@Resource
	private UserDao userDao;

	/**
	 * 
	 * Date 2019-01-22 19:27:06<br>
	 * Description 判断用户是否管理员
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public boolean isAdmin(Long groupId, Long userId) {
		boolean is = false;
		GroupMemberVo gm = groupMemberDAO.getVOByGroupIdUserId(groupId, userId);
		is = (null != gm && GroupMemberPositionEnums.POSITION_ADMIN.getType().equals(gm.getPosition()));
		return is;
	}

	/**
	 * 
	 * Date 2019-01-22 19:30:56<br>
	 * Description 判断是否群主
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public boolean isOwner(Long groupId, Long userId) {
		boolean is = false;
		GroupMemberVo gm = groupMemberDAO.getVOByGroupIdUserId(groupId, userId);
		is = (null != gm && GroupMemberPositionEnums.POSITION_OWNER.getType().equals(gm.getPosition()));
		return is;
	}

	/**
	 * 
	 * Date 2019-01-26 10:08:58<br>
	 * Description 获取用户在群中的权限
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public String getPosition(Long groupId, Long userId) {
		GroupMemberVo gm = groupMemberDAO.getVOByGroupIdUserId(groupId, userId);
		String position = (null == gm) ? "" : (gm.getPosition());
		return position;
	}

	/**
	 * 
	 * Date 2019-01-26 10:09:17<br>
	 * Description 判断权限是否为管理员
	 * 
	 * @author yitun<br>
	 * @param position
	 * @return
	 * @version 1.1
	 */
	public boolean isAdmin(String position) {
		boolean is = GroupMemberPositionEnums.POSITION_ADMIN.getType().equals(position);
		return is;
	}

	/**
	 * 
	 * Date 2019-01-26 10:09:35<br>
	 * Description 判断权限是否为群主
	 * 
	 * @author yitun<br>
	 * @param position
	 * @return
	 * @version 1.1
	 */
	public boolean isOwner(String position) {
		boolean is = GroupMemberPositionEnums.POSITION_OWNER.getType().equals(position);
		return is;
	}

	/**
	 * 
	 * Date 2019-01-26 23:42:20<br>
	 * Description 普通成员
	 * 
	 * @author yitun<br>
	 * @param position
	 * @return
	 * @version 1.1
	 */
	public boolean isNormal(String position) {
		boolean is = GroupMemberPositionEnums.POSITION_NORMAL.getType().equals(position);
		return is;
	}

	/**
	 * 
	 * Date 2019-01-27 09:30:51<br>
	 * Description 用户是否已经加入群
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public boolean inGroup(Long groupId, Long userId) {
		GroupMemberVo gm = groupMemberDAO.getVOByGroupIdUserId(groupId, userId);
		boolean is = gm != null;
		return is;
	}

	/**
	 * 
	 * Date 2019-01-26 19:31:16<br>
	 * Description 加入群
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	@Transactional
	public GroupMemberDto add(Long groupId, Long userId) {
		String position = GroupMemberPositionEnums.POSITION_NORMAL.getType();// 权限
		return add(groupId, userId, position);
	}

	/**
	 * 
	 * Date 2019-01-26 19:31:29<br>
	 * Description 加入群
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @param userId
	 * @param position
	 * @return
	 * @version 1.1
	 */
	@Transactional
	public GroupMemberDto add(Long groupId, Long userId, String position) {

		GroupMemberDto bean = new GroupMemberDto();
		bean.setGroupId(groupId);
		bean.setUserId(userId);
		bean.setPosition(position);
		bean.setNickname(userDao.get(userId).getNickname()); //设置用户昵称
		bean.setCreateBy(userId);
		bean.setUpdateBy(userId);
		groupMemberDAO.save(bean);
		return bean;
	}

	/**
	 * Date 2019-01-28 23:04:53<br>
	 * Description 获取群成员id
	 * @author yitun<br>
	 * @param groupId
	 * @return
	 * @version 1.1
	 */
	public List<Long> getGroupMemberUserIdList(Long groupId) {
		return groupMemberDAO.listVO(R.param("groupId", groupId)).stream().map(GroupMemberVo::getUserId).collect(Collectors.toList());
	}

	/**
	 * 
	 * Date 2019-01-28 23:04:26<br>
	 * Description 获取群主和管理员id
	 * 
	 * @author yitun<br>
	 * @param groupId
	 * @return
	 * @version 1.1
	 */
	public List<Long> getGroupAdminAndOwnerUserIdList(Long groupId) {
		List<Long> userIds = new ArrayList<>();
		List<GroupMemberVo> list = groupMemberDAO.listVO(R.param("groupId", groupId));
		for (GroupMemberVo member : list) {
			if (GroupMemberPositionEnums.POSITION_ADMIN.getType().equals(member.getPosition())) {
				userIds.add(member.getUserId());
			}
			if (GroupMemberPositionEnums.POSITION_OWNER.getType().equals(member.getPosition())) {
				userIds.add(member.getUserId());
			}
		}
		return userIds;
	}

	/**
	 * 
	 * Description 获取群主 <br>
	 * Date 2019-06-13 09:06:20<br>
	 * 
	 * @param groupId
	 * @return String
	 * @version 1.1
	 */
	public Long getGroupOwnerUserId(Long groupId) {
		Long userId = null;
		List<GroupMemberVo> list = groupMemberDAO.listVO(R.param("groupId", groupId));
		for (GroupMemberVo member : list) {
			if (GroupMemberPositionEnums.POSITION_OWNER.getType().equals(member.getPosition())) {
				userId = (member.getUserId());
				break;
			}
		}
		return userId;
	}
}
